<template>
  <div>
    <el-dialog id="achieve_dialog-sixcopy1" :title="title" :visible.sync="dialogVisible" append-to-body @open="open"
               @close="close">
      <el-form class="popup-one" ref="form" :model="form" :rules="rules" label-width="90px" @submit.native.prevent>
        <el-form-item label="移动位置:" prop="position">
          <el-select v-model="form.position" filterable placeholder="请选择要调整的位置" clearable
                     style="width: 240px">
            <el-option v-for="item in selectlist" :key="item.id" :label="item.middle" :value="item.sort"/>
          </el-select>
        </el-form-item>
      </el-form>
      <div>
        <div class="move">移动任务信息</div>
        <div v-for="(item,index) in this.checked" :key="item.id" class="indetail">
          工单号{{ item.orderNumber }},图号:{{ item.productCode }},工单总量：{{ item.orderProductCount }}
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button class="function-two" @click="cancel">取 消</el-button>
        <el-button class="function-two" type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {getWaitingsuspenddataOut, movedata} from "@/api/prodManger/outProdplan";

export default {
  name: "EditControl",
  props: {
    title: {
      type: String,
      default: '',
    },
    linedata: {
      type: Array,
      default: null,
    },
    ids: {
      type: Array,
      default: null,
    },
  },
  data() {
    return {
      checked: [],
      selectlist: null,
      dialogVisible: null,//控制弹框是否展示
      form: {},//列表数据
      rules: {//校验
        position: [{required: true, message: '请选择要移动的位置', trigger: 'change'}],
      },
    }
  },
  methods: {
    // 打开弹窗事件
    open() {
      this.checked = [];
      let grouping = this.handlerArr(this.$props.linedata, ['productCode', 'orderNumber']);
      grouping.map(item => {
        for (let i = 1; i <= item.length - 1; i++) {
          item[0].orderProductCount += item[i].orderProductCount;
        }
      })
      for (let i = 0; i <= grouping.length - 1; i++) {
        this.checked.push(grouping[i][0])
      }
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
      this.getselectList();
    },
    handlerArr(arr, key) {
      const obj = {};
      arr.forEach((item) => {
        let value = Array.isArray(key) ? key.map((el) => item[el]).join() : item[key];
        obj[value] ? obj[value].push(item) : (obj[value] = [item]);
      });
      return Object.keys(obj).map((item) => obj[item]);
    },
    getselectList() {
      let plName = this.$props.linedata[0].prodLineName;
      getWaitingsuspenddataOut(plName).then((res) => {
        const filterFun = (arr1, arr2) => {
          const filterArr = arr2.filter(item => arr1.every(ele => ele.id != item.sort))
          return filterArr
        }
        const resultArr = filterFun(this.$props.ids, res.data);
        resultArr.map((item) => {
          item.middle =
            item.sort +
            " | " +
            item.prodLineName +
            " | " +
            item.productCode +
            " | " +
            item.orderNumber +
            " | " +
            item.orderProductCount +
            " | " +
            this.parseTime(item.sendTime);
        });
        this.selectlist = resultArr;
      });
    },
    // 确定事件
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (this.$props.linedata.length > 1) {
          let obj = {};
          let file = this.selectlist.filter(item => {
            return item.sort == this.form.position
          })
          let old = file.map(item => {
            return {"id": item.id, "sort": item.sort}
          })
          let size = this.$props.linedata.map(item => {
            return item.sort
          })
          let sizesort = size.sort((a, b) => a - b);
          let first = sizesort[0];
          let last = sizesort[sizesort.length - 1];
          let begin = first <= old[0].sort;
          let end = old[0].sort <= last
          if (begin && end) {
            this.$message.warning('移动位置不能在选中区间内');
            return;
          }
          let checkeddata = this.$props.linedata.map(item => {
            return {"id": item.id, "sort": item.sort}
          })
          if (old[0].sort < first) {
            obj.typeFlag = 0
          }
          if (old[0].sort > last) {
            obj.typeFlag = 1
          }
          obj.movePoint = old[0];
          obj.plname = this.$props.linedata[0].prodLineName;
          obj.moveTasks = checkeddata;
          movedata(obj).then(res => {
            this.$message.success('移动成功')
            this.dialogVisible = false;
            this.$emit('refresh')
            this.reset();
          })
        } else {
          let obj = {};
          let file = this.selectlist.filter(item => {
            return item.sort == this.form.position
          })
          let old = file.map(item => {
            return {"id": item.id, "sort": item.sort}
          })
          let checkeddata = this.$props.linedata.map(item => {
            return {"id": item.id, "sort": item.sort}
          })
          if (this.form.position == this.$props.linedata[0].sort) {
            this.$message.warning('移动位置不能跟原始位置相同')
            return;
          }
          if (this.form.position > this.$props.linedata[0].sort) {
            obj.typeFlag = 1
          } else {
            obj.typeFlag = 0
          }
          obj.movePoint = old[0];
          obj.moveTasks = checkeddata;
          obj.plname = this.$props.linedata[0].prodLineName;
          movedata(obj).then(res => {
            this.$message.success('移动成功')
            this.dialogVisible = false;
            this.$emit('refresh')
            this.reset();
          })
        }
      });
    },
    // 重置
    reset() {
      this.form.position = null;
    },
    // 关闭事件
    close() {
      this.reset();
    },
    // 取消事件
    cancel() {
      this.dialogVisible = false;
      this.reset();
    },
  },
}
</script>

<style scoped>
.move {
  margin: 0 0 22px 7px;
}

.indetail {
  margin: 0 0 5px 7px;
}

.dialog-footer {
  padding-right: 20px;
}

.popup-one {
  margin-left: -45px;
}
</style>
